package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengLessonDetailController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository schoolLessonRepository;

    @Autowired
    YxyLessonsSchoolLessonChapterRepository chapterRepository;

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyLessonsSchoolCatalogRepository catalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/lesson/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;

        RespModel respModel = new RespModel();

        YxyLessonsSchoolLesson yxyLessonsSchoolLesson = schoolLessonRepository.findById(lessonId);
        if (yxyLessonsSchoolLesson != null) {
            String id = lessonId;
            String name = yxyLessonsSchoolLesson.getName();
            String cover = yxyLessonsSchoolLesson.getCover();
            String author = yxyLessonsSchoolLesson.getAuthor();
            Integer count = yxyLessonsSchoolLesson.getCount();
            Integer duration = yxyLessonsSchoolLesson.getDuration();
            String summary = yxyLessonsSchoolLesson.getSummary();
            Date date = yxyLessonsSchoolLesson.getCreateDate();
            String catalogId1 = yxyLessonsSchoolLesson.getCatalogId1();
            String catalogId2 = yxyLessonsSchoolLesson.getCatalogId2();
            Date lastModifiedDate = yxyLessonsSchoolLesson.getLastModifiedDate();
            Date upDate = yxyLessonsSchoolLesson.getUpDate();
            Date downDate = yxyLessonsSchoolLesson.getDownDate();
            Date rejectDate = yxyLessonsSchoolLesson.getRejectDate();
            String vv = yxyLessonsSchoolLesson.getVv() + "";
            long flag = lastModifiedDate.getTime() - date.getTime();
            String downType = yxyLessonsSchoolLesson.getDownType();

            YxyLessonsSchoolCatalog catalog1 = catalogRepository.findById(catalogId1);
            YxyLessonsSchoolCatalog catalog2 = catalogRepository.findById(catalogId2);

            respModel.id = id;
            respModel.name = name;
            respModel.cover = cover;
            respModel.author = author;
            respModel.count = count;
            respModel.duration = disDuration(duration);
            respModel.summary = summary;
            respModel.vv = vv;
            respModel.flag = flag;
            respModel.downReason = yxyLessonsSchoolLesson.getDownReason();
            respModel.downType = downType;
            respModel.rejectReason = yxyLessonsSchoolLesson.getRejectReason();

            if (catalog1 != null) {
                respModel.catalog1Name = catalog1.getName();
            }
            if (catalog2 != null) {
                respModel.catalog2Name = catalog2.getName();
            }
            if (date != null) {
                respModel.createDate = DateToolUtils.format(date, "yyyy-MM-dd");
            }
            if (null != lastModifiedDate) {
                respModel.lastModifiedDate = DateToolUtils.format(lastModifiedDate, "yyyy-MM-dd");
            }
            if (null != upDate) {
                respModel.upDate = DateToolUtils.format(upDate, "yyyy-MM-dd");
            }
            if (null != downDate) {
                respModel.downDate = DateToolUtils.format(downDate, "yyyy-MM-dd");
            }
            if (null != rejectDate) {
                respModel.rejectDate = DateToolUtils.format(rejectDate, "yyyy-MM-dd");
            }

            // 获取课程章节
            List<YxyLessonsSchoolLessonChapter> chapters = chapterRepository.findByLessonId(lessonId);
            List<Chapter> chapterList = new ArrayList<Chapter>();
            for (YxyLessonsSchoolLessonChapter chapter : chapters) {
                Chapter c = new Chapter();
                String chapterId = chapter.getId();
                c.title = chapter.getTitle();
                c.intro = chapter.getIntro();
                List<Item> itemList = new ArrayList<>();
                // 获取章节对应的课时
                List<YxyLessonsSchoolLessonItem> items = itemRepository.findByChapterIdAndStatusNotOrderByOrderNumAsc(chapterId, -1);
                for (YxyLessonsSchoolLessonItem it : items) {
                    Item item = new Item();
                    item.itemId = it.getId();
                    item.title = it.getTitle();
                    item.intro = it.getIntro();
                    item.status = it.getStatus();
                    item.orderNum = it.getOrderNum();
                    item.rejectReason = it.getRejectReason();
                    if (it.getLastModifiedDate() != null && it.getCreatedDate() != null) {
                        item.flag = it.getLastModifiedDate().getTime() - it.getCreatedDate().getTime();
                    }
                    // 课时没有转码成功
                    if (it.getStatus() != 2) {
                        respModel.allItemStatus2 = false;
                    }
                    // 视频id
                    String videoId = it.getVideoId();
                    YxyLessonsVideo lessonsVideo = videoRepository.findById(videoId);
                    if (lessonsVideo != null) {
                        Video video = new Video();
                        video.videoId = lessonsVideo.getId();
                        video.fileName = lessonsVideo.getFileName();
                        video.fileSize = lessonsVideo.getFileSize();
                        video.location = lessonsVideo.getLocation();
                        video.duration = lessonsVideo.getDuration();
                        video.extension = lessonsVideo.getExtension();
                        video.md5 = lessonsVideo.getMd5();
                        video.status = lessonsVideo.getStatus();
                        item.count = disDuration(lessonsVideo.getDuration());
                        item.video = video;
                    }
                    itemList.add(item);
                }
                c.itemList = itemList;
                chapterList.add(c);
            }
            respModel.chapterList = chapterList;
        }
        return respModel;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String lessonId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "未获取课程");
        }
    }

    public static class Video {
        public String videoId;
        public String fileName;
        public Integer fileSize;
        public Integer duration;
        public String extension;
        public String location;
        public String md5;
        public Integer status;
        public String userName;
    }

    public static class Item {
        public String itemId;
        public String title;
        public String intro;
        public Integer status;
        public String count;
        public Video video;
        public long flag;
        public Integer orderNum;
        public String rejectReason;
    }

    public static class Chapter {
        public String title;
        public String intro;
        public List<Item> itemList;
    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
        public String name;
        public String cover;
        public String author;
        public Integer count;
        public String duration;
        public String summary;
        public String createDate;
        public String catalog1Name;
        public String catalog2Name;
        public List<Chapter> chapterList;
        public String lastModifiedDate;
        public String upDate;
        public String downDate;
        public String rejectDate;
        public String vv;
        public long flag;
        public String downType;
        public String downReason;
        public String rejectReason;
        public boolean allItemStatus2 = true;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
